mod api;
mod response;
mod handler;

use actix_web::{web, App, HttpServer, HttpResponse, Responder, post, get, error};
use serde::{Serialize, Deserialize};
use std::io::Write;
use actix_web::http::StatusCode;
use actix_web::middleware::ErrorHandlers;
use log::error;
use crate::api::login_api;
use crate::api::folder_api;
use crate::handler::error_handler;


#[actix_web::main]
async fn main() -> std::io::Result<()> {

    // // custom `Json` extractor configuration
    // let json_cfg = web::JsonConfig::default()
    //     // limit request payload size
    //     .limit(4096)
    //     // only accept text/ plain content type
    //     .content_type(|mime| mime == mime::TEXT_PLAIN)
    //     // use custom error handler
    //     // .error_handler(|err, req| {
    //     //     error::InternalError::from_response(err, HttpResponse::Conflict().into()).into()
    //     // });
    //     .error_handler(web::JsonConfig::default().error_handler(error_handler::handle_json_error));

    HttpServer::new(move || {
        App::new()
            .service(login_api::login)
            .service(folder_api::set_default_storage_path)
            .app_data(
                web::JsonConfig::default()
                    // register error_handler for JSON extractors.
                    .error_handler(error_handler::json_error_handler),
            )
            // .service(web::resource("/").route(web::post().to(HttpResponse::InternalServerError)))
            // .wrap(
            //     ErrorHandlers::new()
            //         .default_handler_client(error_handler::add_error_header) // or .default_handler_server
            //         .handler(StatusCode::BAD_REQUEST, error_handler::add_error_header)
            // )

            // .wrap(web::JsonConfig::default().error_handler(error_handler::handle_json_error)) // 处理 JSON 错误
    })
        .bind("127.0.0.1:8080")?
        .run()
        .await
}
